<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	    
class Data_barang extends Member_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->library(array('table','form_validation','server_processing'));
		$this->load->helper(array('form','url'));
		$this->load->model('data_barang_model','',TRUE);
	}
	public function index()
	{
		$partial['_header']='template/header';
		$partial['_sidebar']="template/sidebar";
		$partial['_contents']="data_barang/data_barang";	
		$data['title']='Data Barang';
		//set notiication
		$notif = $this->uri->segment(3);
		if($notif == '1')
		$data['notif'] = 'Data berhasil ditambah';
		else if($notif == '2')
		$data['notif'] = 'Data berhasil dihapus';
		else if($notif == '3')
		$data['notif'] = 'Update data success';
		else
		$data['notif'] = '';
	
		$this->template->load('index',$partial,$data);
	}
	
	function cek_id(){
		$id = $this->input->post('id_barang');
		if(!empty($id)){
			$check=$this->data_barang_model->cek_id($id);
			if($check->num_rows == 0)
				echo "true";
			else
				echo "false";
		}
		else
			echo "false";
	}
	function cek_name(){
		$name = $this->input->post('nama_barang');
		if(!empty($name)){
			$check=$this->data_barang_model->cek_name($name);
			if($check->num_rows == 0)
				echo "true";
			else
				echo "false";
		}
		else
			echo "false";
	}
	function cek_not_name(){
		$name = $this->input->post('nama_barang');
		if(!empty($name)){
			$check=$this->data_barang_model->cek_name($name);
			if($check->num_rows == 0)
				echo "false";
			else
				echo "true";
		}
		else
			echo "true";
	}
	function add(){
	$partial['_header']='template/header';
	$partial['_sidebar']="template/sidebar";
	$partial['_contents']="data_barang/form_data_barang";
	// set common properties
	$data['title'] = 'Data Barang';
	$data['action'] = site_url('data_barang/add/do');
	$data['form_attr'] = array('class' => 'form-horizontal', 'id' => 'FormBarang', 'method' => 'post');
	$data['link_back'] = anchor('data_barang','Back to list of data barang',array('class'=>'back'));
	$data['disable'] = '';
	$model_name = array('kategori_barang_model');
	$this->load->model($model_name,'',TRUE);
	$data['kategori_barang']=$this->kategori_barang_model->getAll()->result();
	$this->load->model('data_gudang_model','',TRUE);
	$data['nama_bahan_gudang']=$this->data_gudang_model->getName()->result();
	
		if($this->uri->segment(3) === false){
		$data['message']='';
		$data['barang']['id_barang']='';
		$data['barang']['nama_barang']='';
		$data['barang']['id_kategori_barang']='';
		$data['link_back']=anchor('data_barang','Cancel',array('class'=>'back'));
		$this->template->load('index',$partial,$data);
		}
		else{
		//save data
		$id_barang = strtoupper($this->input->post('id_barang'));				
		$barang=array(
		'id_barang'=>$id_barang,
		'nama_barang'=>$this->input->post('nama_barang'),
		'id_kategori_barang'=>$this->input->post('id_kategori_barang'));
		
		$this->data_barang_model->save($barang);

//		$id_barang = mysql_insert_id();
		$langkah = $this->input->post('langkah_kerja');
		
		foreach ($langkah as $key => $value){
		if($value!=NULL){
			$langkah_kerja = array('nama_langkah_kerja' => $value, 'id_barang' => $id_barang);
			$this->data_barang_model->save_langkah($langkah_kerja);
			}
		}
		
		$bahan = $this->input->post('nama_bahan');
		$jumlah = $this->input->post('jumlah');
		
		foreach ($bahan as $key => $value){
		if($value!=NULL){
			$id_bahan = $this->data_gudang_model->get_id_bahan($value)->row();
			$kebutuhan = array('jumlah_kebutuhan' => $jumlah[$key], 'id_barang' => $id_barang, 'id_bahan' => $id_bahan->id_bahan);
			$this->data_barang_model->save_kebutuhan($kebutuhan);
			}
		}
		//redirect to data list page
		redirect('data_barang/index/1');		
		}
	}	

	function update($id){
	
	//set common properties
	$partial['_header']='template/header';
	$partial['_sidebar']="template/sidebar";
	$partial['_contents']="data_barang/form_data_barang";
	
	$data['title'] = 'Data Barang';
	$data['action'] = site_url('data_barang/update/'.$id.'/do');
	$data['form_attr'] = array('class' => 'form-horizontal', 'id' => 'FormBarang');
	$data['disable'] = 'disabled';		
	$model_name = array('kategori_barang_model');
	$this->load->model($model_name,'',TRUE);
	$data['kategori_barang']=$this->kategori_barang_model->getAll()->result();
	$this->load->model('data_gudang_model','',TRUE);
	$data['nama_bahan_gudang']=$this->data_gudang_model->getName()->result();
	
	if(	$this->uri->segment(4) === false){
		$data['message']='';
		$data['barang']=$this->data_barang_model->get_by_id($id)->row_array();
		$this->load->model('kategori_barang_model','',TRUE);
		$data['kategori_barang']=$this->kategori_barang_model->not($data['barang']['id_kategori_barang'])->result();		
		$data['langkah_kerja']=$this->data_barang_model->get_langkah_by_id($id)->result();
		$data['bahan']=$this->data_barang_model->get_bahan_by_id($id)->result();
		$data['link_back']=anchor('data_barang','Cancel',array('class'=>'back'));
		//load view
		$this->template->load('index',$partial,$data);
	}
	else{
		//save data
		$id=$this->input->post('id');
		$barang=array('nama_barang'=>$this->input->post('nama_barang'),
		'id_kategori_barang'=>$this->input->post('id_kategori_barang'));
	
		$this->data_barang_model->update($id,$barang);
		//redirect to data list page
		redirect('data_barang/index/3');			
		}
	}
	
	function delete($id){
	//delete data
	$this->data_barang_model->delete($id);
	//redirect to data list page
	redirect('data_barang/index/2');
	}
	
	function _set_rules(){
	$this->form_validation->set_rules('id_bahan','Id','required');
	}
	
	function server(){
	/* ------inisialisasi tabel------- */
	//jika tidak di inisialisasi isi "", kecuali nama table wajib di isi
	//jika menggunakan join, nama field harus lengkap dengan tabelnya kecuali nama fieldnya uniq
	$field = array( 'id_barang', 'nama_barang', 'nama_kategori_barang');
	$table_name = "barang";
	$uri = $this->uri->segment(1);
	$join = array("kategori_barang");
	$on = array("barang.id_kategori_barang=kategori_barang.id_kategori_barang");
	$init = array('select' => $field, 'table_name' => $table_name,'join' => $join, 'on' => $on, 'uri' => $uri);
	$this->server_processing->server_data($_GET,$init);	
	}
}